import psutil

def bytes_to_gb(bytes_value):
    return round(bytes_value / (1024 ** 3), 2)  # 转成GB，保留两位小数

def get_disk_usage(path):
    try:
        usage = psutil.disk_usage(path)
        return {
            'total_gb': bytes_to_gb(usage.total),
            'used_gb': bytes_to_gb(usage.used),
            'free_gb': bytes_to_gb(usage.free),
            'percent': usage.percent
        }
    except Exception:
        return None  # 路径不存在或访问异常时返回None

def get_system_status():
    status = {}

    # CPU占用百分比
    status['cpu_percent'] = psutil.cpu_percent(interval=1)

    # 内存信息
    vm = psutil.virtual_memory()
    status['virtual_memory'] = {
        'total_gb': bytes_to_gb(vm.total),
        'available_gb': bytes_to_gb(vm.available),
        'used_gb': bytes_to_gb(vm.used),
        'percent': vm.percent
    }

    # 交换内存
    sm = psutil.swap_memory()
    status['swap_memory'] = {
        'total_gb': bytes_to_gb(sm.total),
        'used_gb': bytes_to_gb(sm.used),
        'free_gb': bytes_to_gb(sm.free),
        'percent': sm.percent
    }

    # 磁盘使用，系统盘和/data盘
    status['disk_usage'] = {}
    # 系统盘（根目录'/')
    system_disk = get_disk_usage('/')
    if system_disk:
        status['disk_usage']['system_disk'] = system_disk
    else:
        status['disk_usage']['system_disk'] = 'Unavailable'

    # /data盘
    data_disk = get_disk_usage('/data')
    if data_disk:
        status['disk_usage']['data_disk'] = data_disk
    else:
        status['disk_usage']['data_disk'] = 'Unavailable'

    return status

if __name__ == "__main__":
    import pprint
    pprint.pprint(get_system_status())
